﻿// Example header text. Can be configured in the options.
using System;
using System.Data;
using System.Windows.Forms;
using vtech_formBase;
using vtech_frameWorks.Common;
using vtech_frameWorks.Exceptions;
using vtech_frameWorks.Library;
using vtech_frameWorks.Library.Encryption;

namespace vtech_system.User
{
    public partial class frmAddUser : frmAdd
    {
        #region Delegate
        public delegate void Add();
        public event Add OnAdd = null;
        protected override void FireEvent()
        {
            if (this.OnAdd != null)
            {
                this.OnAdd();
            }
        }

        #endregion

        public frmAddUser()
        {
            this.InitializeComponent();
        }

        #region CheckControl
        protected override bool CheckControl()
        {
            bool flag = false;
            try
            {
                this.errorProvider.Dispose();
                if (this.txtUserName.Text == "")
                {
                    this.errorProvider.SetError(this.txtUserName, "Bạn chưa nhập tên đăng nhập. Xin vui lòng kiểm tra lại");
                    flag = false;
                }
                else
                {
                    if (this.txtPassword.Text == "")
                    {
                        this.errorProvider.SetError(this.txtPassword, "Bạn chưa nhập mật khẩu. Xin vui lòng kiểm tra lại");
                        flag = false;
                    }
                    else
                    {
                        if (!this.txtPassword.Text.Trim().Equals(this.txtRePassword.Text.Trim()))
                        {
                            this.errorProvider.SetError(this.txtPassword, "Mật khẩu không khớp. Xin vui lòng kiểm tra lại");
                            flag = false;
                        }
                        else
                        {
                            flag = true;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ClsInformation.Error(ex.Message);
                flag = false;
            }
            return flag;
        }

        #endregion

        #region Insert
        protected override void Insert()
        {
            try
            {
                using (DsUser dsUser = new DsUser())
                {
                    ClsUserBS obj = new ClsUserBS();
                    DataRow drow = dsUser.DtUser.NewRow();
                    dsUser.DtUser.Rows.Add(drow);
                    drow["UserName"] = this.txtUserName.Text.Trim();
                    drow["Password"] = ClsMd5.Encrypt(this.txtPassword.Text.Trim(), ClsGlobal.strPrivateKey, true);
                    drow["FullName"] = this.txtFullName.Text.Trim();
                    drow["Description"] = this.txtDescription.Text.Trim();
                    drow["Email"] = this.txtEmail.Text.Trim();
                    drow["Website"] = this.txtWebsite.Text.Trim();
                    drow["WorkPhone"] = this.txtWorkPhone.Text.Trim();
                    drow["HomePhone"] = this.txtHomePhone.Text.Trim();
                    drow["MobilePhone"] = this.txtMobilePhone.Text.Trim();
                    drow["Fax"] = this.txtFax.Text.Trim();
                    drow["WorkAddress"] = this.txtWorkAddress.Text.Trim();
                    drow["HomeAddress"] = this.txtHomeAddress.Text.Trim();
                    drow["Photo"] = ClsImages.ImageToByteArray(this.pbAvatar.Image);
                    drow["Inactive"] = 0;
                    drow["IsOnline"] = 0;
                    obj.DoInsert(dsUser.DtUser);
                }
            }
            catch (Exception ex)
            {
                ClsInformation.Error(ex.ToString());
            }
        }

        #endregion
        
        private void btnSelect_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "JPG Files(*.JPG)|*.JPG|GIF Files(*.GIF)|*.GIF|PNG Files(*.PNG)|*.PNG";
            if (dlg.ShowDialog(this) == DialogResult.OK)
            {
                this.pbAvatar.ImageLocation = dlg.FileName;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.pbAvatar.Image = global::vtech_system.Properties.Resources.Avatar;
        }
    }
}